package app.fugema.shop.util;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import app.fugema.shop.main.setting.model.CustomRegionBean;

public class CityDataUtil
{
    private static CityDataUtil sInstance;


    public static CityDataUtil getInstance()
    {
        if (sInstance == null)
        {
            sInstance = new CityDataUtil();
        }
        return sInstance;
    }

    //地区 选择 3级 分类
    //provinceId   省
    private List<CustomRegionBean> province1Items = new ArrayList<>();
    //cityId 市
    private ArrayList<ArrayList<CustomRegionBean>> city2Items = new ArrayList<>();
    //districtId 区
    private ArrayList<ArrayList<ArrayList<CustomRegionBean>>> district3Items = new ArrayList<>();

    private CityDataUtil()
    {
        initData();
    }

    /**
     * @return  省市区数据
     */
    private static List<CustomRegionBean> getProvinceInfos()
    {
        String jsonList = UIUtils.getAssetsText("region.json");
        //LogUtil.e("region.json","size = " + jsonList );
        List<CustomRegionBean> regionList =  GsonUtil.json2Obj( jsonList , new TypeToken<List<CustomRegionBean>>(){}.getType());
        //List<CustomRegionBean> regionList = JacksonUtils.getConverList(jsonList,CustomRegionBean.class);

        //LogUtil.e("regionList","size = " + regionList.size() );
        return regionList;
    }

    private void initData()
    {
        this.province1Items = getProvinceInfos();
        //遍历省份（第一级）
        for (int i = 0; i < province1Items.size(); i++)
        {
            //该省的城市列表（第二级）
            ArrayList<CustomRegionBean> cityList = new ArrayList<>();
            //该省的所有地区列表（第三级）
            ArrayList<ArrayList<CustomRegionBean>> areaList = new ArrayList<>();
            CustomRegionBean  provinceTemp = province1Items.get(i);
            //LogUtil.e("provinceTemp",JacksonUtils.transBean2Json(provinceTemp) );
            // 有下一级时
            if( UIUtils.isNotEmpty( provinceTemp.getSub() ) )
            {
                for (CustomRegionBean city : provinceTemp.getSub())
                {
                    //添加城市
                    cityList.add( city );
                    //LogUtil.e("json",JacksonUtils.transBean2Json(city) );
                    //该城市的所有地区列表
                    ArrayList<CustomRegionBean> cityAreaList = new ArrayList<CustomRegionBean>();
                    //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                    if (  UIUtils.isEmpty(city.getSub()) )
                    {
                        //无地区数据
                        //city.getArea() == null || city.getArea().size() == 0
                        CustomRegionBean empty = new CustomRegionBean();
                        empty.setName("NA");
                        cityAreaList.add(empty);
                    }
                    else
                    {
                        cityAreaList.addAll(city.getSub());
                    }
                    //添加该省所有地区数据
                    areaList.add(cityAreaList);
                }
            }

            city2Items.add( cityList );
            district3Items.add(areaList);
        }
    }

    public List<CustomRegionBean> getProvince1Items() {
        return province1Items;
    }

    public ArrayList<ArrayList<CustomRegionBean>> getCity2Items() {
        return city2Items;
    }

    public ArrayList<ArrayList<ArrayList<CustomRegionBean>>> getDistrict3Items() {
        return district3Items;
    }
}
